package com.itlong.cloud.operate.service.impl;

import com.itlong.cloud.POJO.DTO.operate.OperateLoggerReleaseLogGetPageDTO;
import com.itlong.cloud.POJO.PO.OperateReleaseLogPO;
import com.itlong.cloud.POJO.VO.operate.OperateReleaseLogVO;
import com.itlong.cloud.PlatformConstants;
import com.itlong.cloud.enumerate.DataStatusEnum;
import com.itlong.cloud.operate.dao.IOperateLoggerReleaseLogDao;
import com.itlong.cloud.operate.service.IOperateLoggerReleaseLogService;
import com.itlong.cloud.utils.data.handler.SqlUtil;
import com.itlong.cloud.utils.page.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.Map;

/**
 * <desc>
 *      运营-日志管理-发布日志业务处理接口实现类
 * </desc>
 *
 * @createDate 2018/04/10
 */
@Service
public class OperateLoggerReleaseLogServiceImpl implements IOperateLoggerReleaseLogService {
    @Autowired
    IOperateLoggerReleaseLogDao iOperateLoggerReleaseLogDao;

    /**
     * <desc>
     *      获取分页数据
     * </desc>
     *
     * @param pageDTO 查询参数DTO
     * @return page模型
     * @author Qiang.S
     * @createDate 2018/04/10
     */
    @Override
    public Page<OperateReleaseLogVO> getPage(OperateLoggerReleaseLogGetPageDTO pageDTO) throws Exception {
        //将DTO转换为Map params
        Map<String, Object> params = SqlUtil.convertBeanToMapParams(pageDTO);
        //分页类默认pageSize为20
        Page<OperateReleaseLogVO> page = new Page<>();
        page.getPage().setPageSize(pageDTO.getPageSize());
        page.getPage().setCurrentPage(pageDTO.getCurrentPage());
        params.put("currentPage", page.getPage().getStartSlip());
        params.put("pageSize", page.getPage().getPageSize());
        //填充返回数据集
        page.setRows(iOperateLoggerReleaseLogDao.getPage(params), iOperateLoggerReleaseLogDao.getPageCount(params));
        return page;
    }

    /**
     * <desc>
     *      保存功能
     * </desc>
     *
     * @param operateReleaseLogPO 发布日志实体
     * @return sql执行行数
     * @author Qiang.S
     * @createDate 2018/04/10
     */
    @Override
    @Transactional(rollbackFor = Exception.class,propagation = Propagation.REQUIRED)
    public Integer save(OperateReleaseLogPO operateReleaseLogPO) throws Exception {
        operateReleaseLogPO.setStatus(DataStatusEnum.NORMAL.getType());
        operateReleaseLogPO.setCreateTime(new Date());
        return iOperateLoggerReleaseLogDao.save(SqlUtil.durableData(operateReleaseLogPO, PlatformConstants.TABLE_SAVE));
    }

    /**
     * <desc>
     *      更新功能
     * </desc>
     *
     * @param operateReleaseLogPO 发布日志实体
     * @return sql执行行数
     * @author Qiang.S
     * @createDate 2018/04/10
     */
    @Override
    @Transactional(rollbackFor = Exception.class,propagation = Propagation.REQUIRED)
    public Integer update(OperateReleaseLogPO operateReleaseLogPO) throws Exception {
        operateReleaseLogPO.setUpdateTime(new Date());
        return iOperateLoggerReleaseLogDao.update(operateReleaseLogPO);
    }
}
